package org.tacademy.and12thjyh.ListViewTest;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class MyAdapter extends BaseAdapter implements ItemView.OnImageClickListener{
	ArrayList<MyData> mList;
	Context mContext;
	OnItemImageClickListener mListener;
	public interface OnItemImageClickListener {
		public void onItemImageClick(MyData data);
	}
	public void setOnItemImageClickListener(OnItemImageClickListener listener) {
		mListener = listener;
	}
	public MyAdapter (Context context, ArrayList<MyData> list){
		mContext = context;
		mList = list;
	}
	public void add (MyData data) {
		mList.add(data);
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		return mList.size();
	}
	@Override
	public Object getItem (int position) {
		return mList.get(position);
	}
	@Override
	public long getItemId(int position){
		return position;
	}
	@Override
	public View getView(int positon, View convertView, ViewGroup parent) {
		ItemView v = new ItemView(mContext);
		if (convertView == null) {
			v= new ItemView(mContext);
			v.setOnImageClickListener(this);
		} else {
			v= (ItemView) convertView;
		}
		MyData item = mList.get(positon);
		v.setData(item);
		return v;
	}
	public void onImageClick(MyData data) {
		if(mListener != null) {
			mListener.onItemImageClick(data);
		}
	}
}
